package com.keba.lamp.oauth.biz;

import cn.hutool.core.bean.BeanUtil;
import com.keba.lamp.base.vo.save.user.BaseEmployeeSaveVO;
import com.keba.lamp.model.enumeration.base.ActiveStatusEnum;
import com.keba.lamp.oauth.service.UserInfoService;
import com.keba.lamp.oauth.vo.param.RegisterByMobileVO;
import com.keba.lamp.system.service.application.DefUserApplicationService;
import com.keba.lamp.system.vo.save.application.DefUserApplicationSaveVO;
import com.keba.lamp.tenant.entity.DefTenantAppConfig;
import com.keba.lamp.tenant.service.DefTenantAppConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.keba.basic.context.ContextUtil;
import com.keba.lamp.base.entity.user.BaseEmployee;
import com.keba.lamp.base.service.user.BaseEmployeeService;
import com.keba.lamp.base.vo.result.user.BaseEmployeeResultVO;
import com.keba.lamp.common.constant.AppendixType;
import com.keba.lamp.file.service.AppendixService;
import com.keba.lamp.model.vo.result.AppendixResultVO;
import com.keba.lamp.oauth.vo.result.DefUserInfoResultVO;
import com.keba.lamp.system.entity.application.DefApplication;
import com.keba.lamp.system.entity.tenant.DefUser;
import com.keba.lamp.system.service.application.DefApplicationService;
import com.keba.lamp.system.service.tenant.DefUserService;
import com.keba.lamp.system.vo.result.application.DefApplicationResultVO;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 用户大业务
 *
 * @author zuihou
 * @date 2021/10/28 13:09
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OauthUserBiz {
    private final BaseEmployeeService baseEmployeeService;
    private final DefUserService defUserService;
    private final DefApplicationService defApplicationService;
    private final AppendixService appendixService;
    private final UserInfoService userInfoService;
    private final DefUserApplicationService defUserApplicationService;


    public DefUserInfoResultVO getUserById(Long id) {
        // 查默认库
        DefUser defUser = defUserService.getByIdCache(id);
        if (defUser == null) {
            return null;
        }

        // 用户信息
        DefUserInfoResultVO resultVO = new DefUserInfoResultVO();
        BeanUtil.copyProperties(defUser, resultVO);

        // 用户头像
        AppendixResultVO appendix = appendixService.getByBiz(defUser.getId(), AppendixType.System.DEF__USER__AVATAR);
        if (appendix != null) {
            resultVO.setAvatarId(appendix.getId());
        }

        resultVO.setTenantId(ContextUtil.getTenantId());

        if (!ContextUtil.isEmptyBasePoolNameHeader() && !ContextUtil.isEmptyEmployeeId()) {
            Long employeeId = ContextUtil.getEmployeeId();
            resultVO.setEmployeeId(employeeId);

            //查 租户库
            BaseEmployee employee = baseEmployeeService.getByIdCache(employeeId);
            resultVO.setBaseEmployee(BeanUtil.toBean(employee, BaseEmployeeResultVO.class));

        }

        DefApplication defApplication = defApplicationService.getDefApp(id);
        resultVO.setDefApplication(BeanUtil.toBean(defApplication, DefApplicationResultVO.class));
        return resultVO;
    }

    @Transactional(rollbackFor = Exception.class)
    public DefUser registerByMobile(RegisterByMobileVO registerByMobileVO) {
        DefUser defUser = userInfoService.registerByMobile(registerByMobileVO);
        baseEmployeeService.save(BaseEmployeeSaveVO.builder().userId(defUser.getId())
                .state(true).realName(defUser.getNickName()).tenantId(registerByMobileVO.getTenantId())
                .activeStatus(ActiveStatusEnum.ACTIVATED.getCode()).build());
        defUserApplicationService.save(DefUserApplicationSaveVO.builder().userId(defUser.getId()).applicationId(ContextUtil.getApplicationId()).build());

        return defUser;
    }
}
